public class Queue {
    //demo22 队列的链式实现
    public class ListNode {
        int val;
        ListNode next;
        ListNode prev;
        ListNode(int x) { val = x; }
    }
    ListNode first;
    ListNode last;
    int size;
    
    public void offer(int x){
        ListNode node = new ListNode(x);
        if(first == null){
            first = node;
        }else {
            last.next = node;
            node.prev = last;
        }
        last = node;
        size++;
    }
    
    public int poll(){
        int result=-1;
        if(first == null){
            return -1;
        } else if (first==last) {
            result = first.val;
            first = null;
            last = null;
            size--;
        }else {
            result = first.val;
            first = first.next;
            first.prev.next = null;
            first.prev = null;
            size--;
        }
        return result;
    }
    public int peek(){
        if(first == null){
            return -1;
        }
        return first.val;
    }
    public boolean isEmpty(){
        return size==0;
    }
    public int size(){
        return size;
    }
}
